org 0100h

BaseOfStack equ 0100h

BaseOfLoadFile equ 08000h
OffsetOfLoadFile equ 0h

%include "common_head.asm"

; 这里和于渊老师的代码不一样，于渊老师在这里有个jmp LABEL_START，并且也把loader做成了可以FAT启动盘一样的格式，我认为没必要
; 所以这里直接从mov ax, cs开始
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp, BaseOfStack

mov dh, 3
push LoadMessage
call DispStr
add sp, 2

xor ah, ah
xor dl, dl
int 13h

push KernelFileName
call findInRootDir
add sp, 2

cmp ax, 0
jz kernel_found
kernel_not_found:
	; 没有找到内核，留后处理
	mov dh, 4
	push Message2
	call DispStr
	add sp, 2
	jmp $
kernel_found:
	and di, 0FFE0h
	add di, 01Ah
	
	mov eax, dword [es:di + 01Ch]
	mov dword [dwKernelSize], eax
	
	mov ax, [es:di]
	push ax
	call ReadFileByFirstSector
	add sp, 2
	cmp ax, 0
	jz loadSuccess
	; 如果加载失败，以后再处理
	jmp $
loadSuccess:
	call KillMotor
	mov dh, 4
	push Message1
	call DispStr
	add sp, 2
	jmp $
	

wSectorNo dw 0
bOdd db 0
dwKernelSize dd 0

KernelFileName db "KERNEL  BIN"
LoadMessage: db "Loading  "
Message1 db "Ready.   "
Message2 db "No KERNEL"

%include "common_funcs.asm"

KillMotor:
	push dx
	mov dx, 03F2h
	mov al, 0
	out dx, al
	pop dx
	ret